package com.qibao.toolkit.exception.strategy.impl;

import com.qibao.toolkit.exception.strategy.AbstractExceptionHandleStrategy;
import com.qibao.toolkit.exception.strategy.BaseExceptionHandleStrategyAware;
import com.qibao.toolkit.exception.strategy.ExceptionHandleType;
import com.qibao.toolkit.web.ResponseModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

/**
 * 400 处理参数错误异常
 *
 * @author wangcheng
 * @version $Id: BadRequestErrorHandleStrategy.java, v0.1 2018/9/26 18:48 wangcheng Exp $$
 */
@ExceptionHandleType({HttpMessageConversionException.class,
         ServletRequestBindingException.class, TypeMismatchException.class,
        MissingServletRequestPartException.class})
public class BadRequestErrorHandleStrategy extends AbstractExceptionHandleStrategy implements
        BaseExceptionHandleStrategyAware {

    private static final Logger logger = LoggerFactory.getLogger(BadRequestErrorHandleStrategy.class);

    @Override
    public ResponseModel doHandle(Throwable throwable) {
        logger.error(throwable.getMessage(), throwable);
        if (throwable.getMessage() != null) {
            return ResponseModel.ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), throwable.getMessage());
        }
        return ResponseModel.ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "error.param.bad");
    }
}
